package com.example.config.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private TokenFilter tokenFilter;
    @Autowired
    private DenyHandle denyHandle;
    @Autowired
    private AuthenticationExceptionHandle authenticationExceptionHandle;
    @Autowired
    private LogoutHandle logoutHandle;
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/nacos/**","/user/login")
                .permitAll()
                .anyRequest().authenticated();
        http.cors().configure(http);
        http.csrf().disable();
        http.addFilterBefore(tokenFilter,UsernamePasswordAuthenticationFilter.class);
        http.exceptionHandling().accessDeniedHandler(denyHandle);
       http.exceptionHandling().authenticationEntryPoint(authenticationExceptionHandle);
    }



    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}
